//
//  DFProductsProductBigPictureController.m
//  DFront
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFProductsProductBigPictureController.h"
#import "UIImageView+WebCache.h"
#import "DFProductsDownLoadView.h"
#import "DFSignlTon.h"
@interface DFProductsProductBigPictureController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)DFProductsDownLoadView *downloadView;

@end

@implementation DFProductsProductBigPictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kScreenHeight / 2 - kScreenWidth * 3 / 8, kScreenWidth, kScreenWidth * 3 / 4 )];
    _scrollView.contentSize = CGSizeMake(kScreenWidth * _picArr.count, kScreenWidth * 3 / 4);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.contentOffset = CGPointMake(kScreenWidth * _index, 0);
    DFSignlTon *signlTon = [DFSignlTon shareInstance];
    for (int i = 0; i < _picArr.count; i++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth * 3 / 4)];
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenWidth * 3 / 4)];
        scrollView.delegate = self;
        scrollView.maximumZoomScale = 2;
        scrollView.bounces = NO;
     // scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenWidth * 3 / 4);
        [scrollView addSubview:image];
        if (signlTon.savemode == SaveModeYes) {
            image.image = [UIImage imageNamed:@""];
        }else
        {
            [image sd_setImageWithURL:[NSURL URLWithString:_picArr[i]]];
        }
            [_scrollView addSubview:scrollView];
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
    [self.view addSubview:_scrollView];
    
    _downloadView = [[DFProductsDownLoadView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50)];
    _downloadView.indexLab.text = [[NSString stringWithFormat:@"%ld",_index + 1] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld",_picArr.count]];
    [_downloadView.downloadBtn addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:_downloadView];
}

- (void)download
{
    NSLog(@"ss");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tap
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _downloadView.indexLab.text = [[NSString stringWithFormat:@"%d",(int)(scrollView.contentOffset.x / kScreenWidth) + 1] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld",_picArr.count]];
    
    for (int i = 0; i < _picArr.count; i++) {
        if (i != (int)(scrollView.contentOffset.x / kScreenWidth)) {
            UIScrollView *scr = _scrollView.subviews[i];
            scr.zoomScale = 1;
        }
    }
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return scrollView.subviews[0];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIImageView *imageview = scrollView.subviews[0];
    
    CGFloat s = scrollView.zoomScale;
    imageview.frame = CGRectMake( 0  ,  0, s*kScreenWidth, s*kScreenWidth*3/4);
    if (scrollView.zoomScale <= 1) {
        scrollView.zoomScale = 1;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
